<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\View;
class AdminAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $aid = session('admin:aid');
        if (!$aid)
        {
            if ($request->ajax() || (isset($_REQUEST['_async']) && 1 == $_REQUEST['_async'])) {
                return output(90001, '', ['redirect' => route('admin/login')]);
            } 
            else
            {
                return redirect()->route('admin/login');
            }
        } else
        {
            View::share('menus', get_my_menu());
            View::share('admin_bash_url', env('ADMIN_URL'));
            return $next($request);
        }
    }
}
